
#include "setting_legal.h"
#include "history_policy.h"
#include "sys_font.h"
#include "pt_fs.h"

static ui_setting_legal_t  ui_set_legal;

static  char * icon_esc_legal[1] = 
{
    "Return.png",
};


static int setting_legal_clicked_event(void)
{

    page_install("setting_policy", lv_page_setting_policy_create(),NULL);
    page_push_anim("setting_policy",LOAD_ANIM_MOVE_LEFT);
    
    return 0;
}

static void setting_legal_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("legal clicked\n");
        setting_legal_clicked_event();
    }
}
static int setting_legal_esc_clicked(lv_event_t* event)
{
    page_pop();
    root_gesture_enable();
    history_policy_set_last(history_policy_get_len()-1);

    return 0;
}

static void setting_legal_esc_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("esc\n");
        setting_legal_esc_clicked(event);
    }
}

int ui_setting_legal_create(lv_obj_t *superview)
{
    int last_index = 0;
    history_policy_t * info = NULL;

    char txt_buff[256];

    ui_set_legal.buff = malloc(4096);
    if(!ui_set_legal.buff){
        debug_e("m err\n");
        return -1;
    }

    ui_set_legal.img_bg = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_legal.img_bg,16,8);
    lv_obj_set_size(ui_set_legal.img_bg,32,32);

	lv_obj_set_style_radius(ui_set_legal.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_legal.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_legal.img_bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_legal.img_bg, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_legal.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_legal.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_legal.img_bg, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_legal.img_bg, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_set_legal.img_bg,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_legal.img_bg,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_legal.img_bg,lv_color_make(0xff,0,0),0);
    #endif 
    //************************
    ui_set_legal.img_esc = lv_img_create(ui_set_legal.img_bg);
    lv_img_set_src(ui_set_legal.img_esc,pt_fs_get(icon_esc_legal[0]));
    lv_obj_set_pos(ui_set_legal.img_esc,0,0);

    lv_obj_add_event_cb(ui_set_legal.img_bg,setting_legal_esc_event_callback,LV_EVENT_ALL,NULL);
    //**********************************************************************************************
    ui_set_legal.title = lv_label_create(superview);
    lv_obj_set_pos(ui_set_legal.title,102,6);
    lv_obj_set_size(ui_set_legal.title,276,25);
    lv_obj_set_style_text_align(ui_set_legal.title,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_legal.title,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_legal.title,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_legal.title, lv_font_get(20), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_legal.title, "法律信息");
    //**********************************************************************************************

    ui_set_legal.version = lv_label_create(superview);
    lv_obj_set_pos(ui_set_legal.version,21,52);
    lv_obj_set_size(ui_set_legal.version,241,25);
    lv_obj_set_style_text_align(ui_set_legal.version,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui_set_legal.version,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_legal.version,lv_color_make(0x99,0x99,0x99),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_legal.version, lv_font_get(18), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,256,"用户协议|隐私条款");

    lv_label_set_text( ui_set_legal.version, txt_buff);
    //**********************************************************************************************
    //soft info

    ui_set_legal.bg_txt = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_legal.bg_txt,21,87);
    lv_obj_set_size(ui_set_legal.bg_txt,437,192);

	lv_obj_set_style_radius(ui_set_legal.bg_txt, 5, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_legal.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_legal.bg_txt, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_legal.bg_txt, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_legal.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_legal.bg_txt, 0, LV_PART_MAIN);
	//lv_obj_set_scrollbar_mode(ui_set_legal.bg_txt, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_legal.bg_txt, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_set_legal.bg_txt,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_legal.bg_txt,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_set_legal.bg_txt,lv_color_make(0,0,0),0);
    #endif 

    //txt

    ui_set_legal.txt = lv_label_create(ui_set_legal.bg_txt);
    lv_obj_set_pos(ui_set_legal.txt,0,0);

    lv_obj_set_style_text_align(ui_set_legal.txt,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui_set_legal.txt,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_legal.txt,lv_color_make(0x99,0x99,0x99),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_legal.txt, lv_font_get(18), LV_STATE_DEFAULT);

    lv_obj_set_width(ui_set_legal.txt, 420);
    //lv_obj_set_height(ui_set_legal.txt, 226);
    lv_label_set_long_mode(ui_set_legal.txt, LV_LABEL_LONG_WRAP);

    #if 0
    lv_obj_set_style_bg_opa(ui_set_legal.txt,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_legal.txt,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_set_legal.txt,lv_color_make(0,0,0),0);
    #endif 

    memset(ui_set_legal.buff,0,sizeof(4096));

    last_index = history_policy_get_last();
    info = history_policy_get_by_index(last_index);
    if(!info){
        snprintf(ui_set_legal.buff,4096,"   ");
    }else{
        snprintf(ui_set_legal.buff,4096,info->e_attr.buff);
    }
    lv_label_set_text(ui_set_legal.txt, ui_set_legal.buff);
    //**********************************************************************************************
    //update btn

    ui_set_legal.bg_update = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_legal.bg_update,21,285);
    lv_obj_set_size(ui_set_legal.bg_update,241,25);

	lv_obj_set_style_radius(ui_set_legal.bg_update, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_legal.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_legal.bg_update, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_legal.bg_update, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_legal.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_legal.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_legal.bg_update, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_legal.bg_update, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_set_legal.bg_update,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_legal.bg_update,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_legal.bg_update,lv_color_make(0x0,0x0,0x0),0);
    #endif 

    lv_obj_add_event_cb(ui_set_legal.bg_update, setting_legal_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_set_legal.txt_update = lv_label_create(ui_set_legal.bg_update);
    lv_obj_set_pos(ui_set_legal.txt_update,0,0);
    lv_obj_set_size(ui_set_legal.txt_update,241,25);
    lv_obj_set_style_text_align(ui_set_legal.txt_update,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui_set_legal.txt_update,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_legal.txt_update,lv_color_make(0x26,0x7a,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_legal.txt_update, lv_font_get(18), LV_STATE_DEFAULT);

    lv_label_set_text(ui_set_legal.txt_update,"点击更多历史隐私条款");
    //**********************************************************************************************

    return 0;
}

int ui_setting_legal_destroy(lv_obj_t *superview)
{
    if(ui_set_legal.img_bg){
        lv_obj_del(ui_set_legal.img_bg);
        ui_set_legal.img_bg = NULL;
    }
    if(ui_set_legal.title){
        lv_obj_del(ui_set_legal.title);
        ui_set_legal.title = NULL;
    }
    if(ui_set_legal.version){
        lv_obj_del(ui_set_legal.version);
        ui_set_legal.version = NULL;
    }
    if(ui_set_legal.bg_txt){
        lv_obj_del(ui_set_legal.bg_txt);
        ui_set_legal.bg_txt = NULL;
        ui_set_legal.txt = NULL;
    }
    if(ui_set_legal.bg_update){
        lv_obj_del(ui_set_legal.bg_update);
        ui_set_legal.bg_update = NULL;
        ui_set_legal.txt_update = NULL;
    }

    if(ui_set_legal.buff){
        free(ui_set_legal.buff);
        ui_set_legal.buff = NULL;
    }

    return 0;
}

static void setting_legal_on_view_did_load(page_base_t* self)
{
    ui_setting_legal_create(self->root);
}

static void setting_legal_on_view_did_unload(page_base_t* self)
{
    ui_setting_legal_destroy(self->root);
}




static page_vtable_t setting_legal_param = {
    .on_custom_attr_config  = NULL,
    .on_view_load           = NULL,
    .on_view_did_load       = setting_legal_on_view_did_load,
    .on_view_will_appear    = NULL,
    .on_view_did_appear     = NULL,
    .on_view_will_disappear = NULL,
    .on_view_did_disappear  = NULL,
    .on_view_did_unload     = setting_legal_on_view_did_unload,
};


page_vtable_t* lv_page_setting_legal_create(void)
{
    return &setting_legal_param;
}
